package servlet.huwenyao;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import pojo.Order;
import service.OrderService;
import service.impl.OrderServiceImpl;
import util.JsonDateValueProcessor;
import util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(value = "/SeleOrderServlet")
public class SeleOrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json;charset=UTF-8");
        PageBean pb = new PageBean();
        //显示的页码
        String pageIndex = request.getParameter("page");
        if (null == pageIndex) {
            pb.setPageIndex(1);
        } else {
            pb.setPageIndex(Integer.valueOf(pageIndex));
        }
        //每页显示的记录数
        String pageSize = request.getParameter("limit");
        if (null == pageSize) {
            pb.setPageSize(10);
        } else {
            pb.setPageSize(Integer.parseInt(pageSize));
        }
        //接受查询的参数
        OrderService orderService=new OrderServiceImpl();
        pb=orderService.orderPageList(pb,"",0);

        //创建json对象
        JSONObject json=new JSONObject();
        json.put("code",0);//状态码
        json.put("msg",""); //附带消息
        //把list转换成json数组
        JsonConfig config=new JsonConfig();
        config.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
        JSONArray array=JSONArray.fromObject(pb.getList(),config);
        json.put("data",array);
        PrintWriter out = response.getWriter();
        out.print(json);
        /* response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("UTF-8");
        //实例化service层
        OrderService o=new OrderServiceImpl();
        Order order=new Order();
        List<Order> orderList=new ArrayList<Order>();
        orderList= o.orderList();
        JSONObject json=new JSONObject();
        json.put("code",0);//状态码
        json.put("msg",""); //附带消息
        //把list转换成json数组
        JsonConfig config=new JsonConfig();
        config.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
        JSONArray array=JSONArray.fromObject(orderList,config);
        json.put("data",array);
        PrintWriter out = response.getWriter();
        out.print(json);*/
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request,response);
    }
}
